import { http } from "@/utils/http";

// 车辆车位绑定记录
export interface VehicleSpace {
  id: number;
  cellId: number;
  vehicleId: number;
  parkingSpaceId: number;
  bindType: number;
  authorizeStart: string;
  authorizeEnd: string;
  status: number;
  createdBy?: string;
  createdTime: string;
  updatedTime: string;
  tenantId: number;
}

// 车辆接口类型定义
export interface Vehicle {
  id: number; // 车辆ID
  cellId?: number; // 小区ID
  cellName: string; // 小区名称
  plateNo: string; // 车牌号
  ownerName: string; // 车主姓名
  contactPhone: string; // 联系电话
  ownerType?: number; // 车主类型
  remark?: string; // 备注
  vehicleSpaces: VehicleSpace[]; // 车辆车位绑定记录列表
  [key: string]: any;
}

// 添加车辆请求参数
export interface CreateVehicleRequest {
  cellId: number; // 小区ID
  plateNo: string; // 车牌号
  ownerName: string; // 车主姓名
  contactPhone: string; // 联系电话
  ownerType: number; // 车主类型
  remark?: string; // 备注
}

// 车辆查询参数
export interface VehicleQueryParams {
  page?: number; // 页码
  limit?: number; // 每页数量
  cellId?: number; // 小区ID
  plateNo?: string; // 车牌号
  ownerName?: string; // 车主姓名
  tenantId?: number; // 租户ID (前端不传，后端自动添加)
}

// 通用响应接口
export interface ApiResponse<T = any> {
  code: number;
  data?: T;
  message: string;
}

// 车辆列表响应
export interface VehicleListResponse
  extends ApiResponse<{
    list: Vehicle[];
    total: number;
    pageNum: number;
    pageSize: number;
    size: number;
    startRow: number;
    endRow: number;
    pages: number;
    prePage: number;
    nextPage: number;
    isFirstPage: boolean;
    isLastPage: boolean;
    hasPreviousPage: boolean;
    hasNextPage: boolean;
    navigatePages: number;
    navigatepageNums: number[];
    navigateFirstPage: number;
    navigateLastPage: number;
  }> { }

// 查询车辆列表
export const getVehicleList = (params?: VehicleQueryParams) => {
  return http.request<VehicleListResponse>("get", "/vehicle/list", {
    params
  });
};

// 根据ID查询车辆信息
export const getVehicleById = (id: number) => {
  return http.request<ApiResponse<Vehicle>>(
    "get",
    `/vehicle/findById?id=${id}`
  );
};

// 创建车辆
export const createVehicle = (data?: CreateVehicleRequest) => {
  return http.request<ApiResponse>("post", "/vehicle/add", { data });
};

// 更新车辆
export const updateVehicle = (data?: Vehicle) => {
  return http.request<ApiResponse>("post", "/vehicle/update", { data });
};

// 删除车辆
export const deleteVehicle = (id: number) => {
  return http.request<ApiResponse>("get", "/vehicle/delete", {
    params: { id }
  });
};

// 批量删除车辆
export const batchDeleteVehicle = (ids: number[]) => {
  return http.request<ApiResponse>("post", "/vehicle/deleteBatch", {
    data: { ids }
  });
};

// 批量导入车辆信息
export const importVehicles = (file: File) => {
  const formData = new FormData();
  formData.append("file", file);
  return http.request<ApiResponse>("post", "/vehicle/import", {
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
};

// 导出车辆Excel模板
export const exportVehicleTemplate = () => {
  return http.request<Blob>("get", "/vehicle/exportTemplate", {
    responseType: 'blob',
  });
};

// 根据车牌号搜索车辆
export const searchVehicleByPlateNo = (plateNo: string) => {
  return http.request<ApiResponse<Vehicle[]>>("get", "/vehicle/search", {
    params: { plateNo },
  });
};
